<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">{{ title }}</h3>
      </div>
      <el-form-item>
        <span class="svg-container">
          <svg-icon icon-class="web" />
        </span>
        <el-select v-model="demo" style="width: 100%" @change="webChange">
          <el-option label="惠州杰希制造公司" value="jc" />
          <el-option label="亚太销售公司" value="nx" />
          <el-option label="摩洛哥餐椅制造公司" value="mlg" />
          <el-option label="jnk制造公司" value="jnk" />
          <el-option label="测试环境" value="demo" />
        </el-select>
      </el-form-item>
      <el-form-item prop="userName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model.trim="loginForm.userName"
          placeholder="用户名"
          name="userName"
          type="text"
          tabindex="1"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model.trim="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom: 30px;"
        @click.native.prevent="handleLogin"
      >
        {{ $t('market.login') }}<!--登录-->
      </el-button>
      <span style="color: white">
        推荐使用Chrome浏览器，可以获得更佳页面操作体验
      </span>
      <a
        href="https://www.google.cn/intl/zh-CN/chrome/"
        style="color: #409eff; text-decoration:underline"
        target="_blank"
      >
        https://www.google.cn/chrome/
      </a>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        userName: '',
        password: ''
      },
      demo: 'jc',
      title: '三幸运营平台',
      loginRules: {
        userName: [
          { required: true, trigger: 'blur', message: '账号不能为空' }
        ],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    if (location.pathname.indexOf('jc-web') !== -1) { // 惠州杰希
      this.demo = 'jc'
      this.title = '三幸运营平台(惠州杰希)'
    } else if (location.pathname.indexOf('mlg-web') !== -1) { // 摩洛哥
      this.demo = 'mlg'
      this.title = '三幸运营平台(摩洛哥)'
    } else if (location.pathname.indexOf('nx-web') !== -1) { // 惠州杰希内销
      this.demo = 'nx'
      this.title = '三幸运营平台(惠州杰希内销)'
    } else if (location.pathname.indexOf('demo-web') !== -1) { // 测试
      this.demo = 'demo'
      this.title = '三幸运营平台(测试)'
    } else if (location.pathname.indexOf('jnk-web') !== -1) { // 摩洛哥-jnk
      this.demo = 'jnk'
      this.title = '三幸运营平台(摩洛哥-jnk)'
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 选择登录网站
    webChange(ev) {
      if (ev === 'mlg') {
        location.href = 'http://182.160.0.111:8808/mlg-web/#/login'
      } else if (ev === 'jnk') {
        location.href = 'http://182.160.0.111:8808/jnk-web/#/login'
      } else {
        location.href = 'http://14.29.79.10:8808/' + ev + '-web/#/login'
      }
    },
    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('login', this.loginForm)
            .then(res => {
              if (res.code !== 0) {
                this.loading = false
                this.$message({ showClose: true, message: res.message, type: 'success' })
                return
              }
              window.sessionStorage.setItem('fuserId', res.data.fuserId)
              window.sessionStorage.setItem('loginToken', res.data.loginToken)
              this.$router.replace({ path: this.redirect || '/' })
              this.$message({ showClose: true, message: `${this.loginForm.userName}${res.message}`, type: 'success' })
              this.loading = false
            })
            .catch(error => {
              this.$message.error(error)
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 48px;
    width: 85%;

    input {
      background: transparent;
      border: 0;
      -webkit-appearance: none;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 48px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    //max-width: 100%;
    padding: 170px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .el-autocomplete {
    width: 100%;
  }
  .title-container {
    position: relative;

    .title {
      font-size: 32px;
      letter-spacing: 2px;
      color: $light_gray;
      margin: 0 auto 30px auto;
      font-weight: bold;
      text-align: center;
      position: relative;
    }
    /** 动画
    .title::after {
      content: '三幸运营平台';
      position: absolute;
      color: #FF3F00;
      top: 0;
      left: 0;
      width: 100%;
      height: 50%;
      //animation: loderWidth 6s linear infinite;
      overflow: hidden;
    } **/
  }
  @keyframes loderWidth {
    0% {
      height: 100%;
    }
    100% {
      height: 50%;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
